package oboe.functors;

import oboe.core.SigSlot;
import oboe.math.Number3D;

import flash.Vector;

class CollisionPoll
{
public var position:Float3D;
private var collisionCorrector:Float3D;
public var collisionData:Vector<CollisionData>;
public var hasCollision:Bool;
public var target:SigSlot;

public function new()
{
	this.collisionData = new Vector();
	this.collisionCorrector = new Number3D(0,0,0);
	this.hasCollision = false;
}

public function reset( target:SigSlot, pos:Float3D ):Void
{
	this.target = target;
	this.position = pos;
	this.collisionData.length = 0;
	this.collisionCorrector.copy(0);
	this.hasCollision = false;
}

public function addCollision( target:SigSlot, pos:Float3D, data:Object=null ):Void
{
	if( this.target == target ) return;
	if( isNaN(pos.x) || isNaN(pos.z) ) 
	{
	trace("NaN corrector colliding with ", Object(target).constructor );
	return;
	}
	this.hasCollision = true;
	this.collisionCorrector.add3( pos );
	this.collisionData.push( new CollisionData( target, pos, data ) );
}

public function calcCorrector():Float3D
{
	this.collisionCorrector.div( this.collisionData.length );
	return this.collisionCorrector;
}
}